package com.example.nvzi;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * 自动任务表
 */
@Component
public class Task {

    @Autowired
    RedisUtil redisUtil;

    /**
     * 自动任务
     */

    @Scheduled(cron ="* 0/10 * * * *" )
    public void getToken(){

        String url=STATIC.getUrl+"/user/login";

        Map<String,String> par=new HashMap<>();


        par.put("username","tongbu123456");

        par.put("password",MD5.md5("tongbu123456"));


        Map<String,String> heard=new LinkedHashMap<>();


        heard.put("Authorization","");

        System.out.println();


        String result=HttpUtils.post(url, JSON.toJSONString(par),"");

        Map<String,Object> resultMap=JSON.parseObject(result,Map.class);

        Object rtn=resultMap.get("rtn");

        System.out.println(result);

        if ((rtn+"").equals("0")){//获取token成功

            STATIC.token=resultMap.get("result")+"";

        }

    }
    /**
     * 收集信息
     */
    @Scheduled(cron ="0/1 * * * * *" )
    public void sendMessage(){

        System.out.println(new Date());

        String url=STATIC.getUrl+"/access/record";


        Map<String,Object> par=new HashMap<>();

        String[] identity_list={"STAFF"};

        //par.put("identity_list",identity_list);//员工

        String[] pass_result_list={"PASS"};

        par.put("pass_result_list",pass_result_list);

        Calendar calendar=Calendar.getInstance();

        calendar.setTime(new Date());


        calendar.add(Calendar.MINUTE,-1);

        String start_timestamp=calendar.getTimeInMillis()+"";

        start_timestamp=start_timestamp.substring(0,10);

        par.put("start_timestamp",Long.parseLong(start_timestamp));




        //par.put("end_timestamp",1575619223);

        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


        System.out.println(simpleDateFormat.format(calendar.getTimeInMillis()));


        String result=HttpUtils.post(url, JSON.toJSONString(par),STATIC.token);

        Map resultMap=JSON.parseObject(result,Map.class);

        String listStr=resultMap.get("result").toString();



        List<Map> list=JSON.parseArray(listStr,Map.class);

        if (list.size()==0){



            System.out.println(simpleDateFormat.format(calendar.getTime()));
        }


        for (int i=0;i<list.size();i++){


            SendBean sendBean=new SendBean();

            Map tempMap=list.get(i);

            String key=MD5.md5(JSON.toJSONString(tempMap));


            if (redisUtil.get(key)==null){//没有推送过


                String personStr=tempMap.get("person").toString();

                String device_id=tempMap.get("device_id").toString();

                sendBean.setDevice_id(device_id);


                Map person=JSON.parseObject(personStr);

                sendBean.setRtn("0");

                sendBean.setType(person.get("identity").toString());

                String cardNumber=((JSONObject) person).getString("card_numbers");

                try {

                    List<String> cardNumberList=JSON.parseArray(cardNumber,String.class);

                    sendBean.setCardId(cardNumberList.get(0));

                }catch (Exception e){

                    System.out.println(personStr);

                    e.printStackTrace();

                }

                //处理图片
                String face_image_id=tempMap.get("face_image_id").toString();

                String imgData=HttpUtilsImg.httpURLConectionGET(STATIC.getUrl+"/image/"+face_image_id);

                sendBean.setPhoto(imgData);

                String sendResult=UtilHttpClient.sendPost

                        (STATIC.sendUrl,JSON.toJSONString(sendBean),"");

                System.out.println(sendResult);



                redisUtil.setEx(key,key,1, TimeUnit.HOURS);

            }else {


                System.out.println("通行记录已推送过");

            }



        }





    }


}
